package com.jt.www.remote;

import com.jt.www.model.reps.GenericListResponse;
import config.FeignConfiguration;
import feign.hystrix.FallbackFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.*;

import java.net.UnknownHostException;
import java.util.Map;

@FeignClient(value = "es-svc", url = "${remote.es_svc_url}",
        configuration = FeignConfiguration.class,
        fallbackFactory = ESClient.FeignClientFallbackFactory.class)
public interface ESClient {

    @GetMapping(value = "/data/{service}/search", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    Map<String, Object> search(
            @PathVariable(value = "service", required = true) String service,
            @RequestParam(value = "where", required = false) String where,
            @RequestParam(value = "pn", required = false, defaultValue = "1") int pn,
            @RequestParam(value = "ps", required = false, defaultValue = "10") int ps,
            @RequestParam(value = "isRePage", required = false, defaultValue = "true") Boolean isRePage
    ) throws UnknownHostException;

    @RequestMapping(method = RequestMethod.GET, value = "/data/{service}/search", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResponseEntity<GenericListResponse> searchLog(@PathVariable(value = "service", required = true) String service,
                                                         @RequestParam(value = "q", required = false) String q,
                                                         @RequestParam(value = "searchFields", required = false) String searchFields,
                                                         @RequestParam(value = "boost", required = false) String boost,
                                                         @RequestParam(value = "mode", required = false) String mode,
                                                         @RequestParam(value = "outFields", required = false, defaultValue = "*") String outFields,
                                                         @RequestParam(value = "where", required = false) String where,
                                                         @RequestParam(value = "pn", required = false, defaultValue = "1") Integer pn,
                                                         @RequestParam(value = "ps", required = false, defaultValue = "10") Integer ps,
                                                         @RequestParam(value = "orderField", required = false) String orderField,
                                                         @RequestParam(value = "orderType", required = false) String orderType,
                                                         @RequestParam(value = "highLight", required = false, defaultValue = "true") boolean highLight,
                                                         @RequestParam(value = "highTags", required = false) String highTags);

    //v2版本增加加密查询
    @RequestMapping(method = RequestMethod.GET, value="api/v2/data/{service}/search",produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResponseEntity<GenericListResponse> searchLogV2(@PathVariable(value = "service", required = true) String service,
                                                         @RequestParam(value = "q", required = false) String q,
                                                         @RequestParam(value = "searchFields", required = false) String searchFields,
                                                         @RequestParam(value = "boost", required = false) String boost,
                                                         @RequestParam(value = "mode", required = false) String mode,
                                                         @RequestParam(value = "outFields", required = false, defaultValue = "*") String outFields,
                                                         @RequestParam(value = "where", required = false) String where,
                                                         @RequestParam(value = "pn", required = false, defaultValue = "1") Integer pn,
                                                         @RequestParam(value = "ps", required = false, defaultValue = "10") Integer ps,
                                                         @RequestParam(value = "orderField", required = false) String orderField,
                                                         @RequestParam(value = "orderType", required = false) String orderType,
                                                         @RequestParam(value = "highLight", required = false, defaultValue = "true") boolean highLight,
                                                         @RequestParam(value = "highTags", required = false) String highTags);

    @Component
    class FeignClientFallbackFactory implements FallbackFactory<ESClient> {

        public static final Logger log = LoggerFactory.getLogger(FeignClientFallbackFactory.class);

        @Override
        public ESClient create(Throwable cause) {
            return new ESClient() {
                @Override
                public Map<String, Object> search(String service, String where, int pn, int ps, Boolean isRePage) throws UnknownHostException {
                    log.error("{}-调用es服务失败,fallback reason:", "search", cause);
                    return null;
                }

                @Override
                public ResponseEntity<GenericListResponse> searchLog(String service, String q, String searchFields, String boost, String mode, String outFields, String where, Integer pn, Integer ps, String orderField, String orderType, boolean highLight, String highTags) {
                    log.error("{}-投保轨迹调用失败,fallback reason:", "svc");
                    return null;
                }

                @Override
                public ResponseEntity<GenericListResponse> searchLogV2(String service, String q, String searchFields, String boost, String mode, String outFields, String where, Integer pn, Integer ps, String orderField, String orderType, boolean highLight, String highTags) {
                    return null;
                }
            };
        }
    }
}
